package org.uiot.protocol.group

import grails.testing.gorm.DomainUnitTest
import org.uiot.protocol.PacketTraits
import spock.lang.Specification

class SequenceSpec extends Specification implements DomainUnitTest<Sequence> {

    void "test valid properties"() {
        given:
        def instance = new Sequence(params)

        when:
        instance.save()
        println instance.errors

        then:
        !instance.hasErrors()

        where:
        params << [
                [part: PacketTraits.Part.Body, parentType: Subjection.Packet, hexParentCode: "A", serialNo: 1, dataIdentifier: "listNo", name: "列表标识号", ruleCode: "listNo"]
        ]
    }

    void "test invalid properties"() {
        given:
        def instance = new Sequence(params)

        when:
        instance.save()

        then:
        instance.hasErrors()

        and:
        instance.errors.allErrors.size() == params.exceptedErrors

        where:
        params << [
                [part: PacketTraits.Part.Header, parentType: Subjection.Packet, hexParentCode: "A", serialNo: 0, dataIdentifier: "listNo.", name: "列表标识号", ruleCode: "listNo", exceptedErrors: 3]
        ]
    }

    void "test as CachedSequence"() {
        given:
        def seq = new Sequence(
                part: PacketTraits.Part.Body, parentType: Subjection.Packet, hexParentCode: "A", serialNo: 1, dataIdentifier: "listNo", name: "列表标识号", ruleCode: "listNo"
        )

        when:
        def cache = seq as CachedSequence

        then:
        ["part", "parentType", "serialNo", "dataIdentifier", "name", "ruleCode"].find {
            seq[it] != cache[it]
        } == null

        and:
        Integer.parseInt(seq.hexParentCode, 16) == cache.parentCode
    }
}
